package com.yjhb.android.sbz.fragment.reward;

import android.os.CountDownTimer;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.reward.GameRewardAdapter;
import com.yjhb.android.sbz.entity.reward.GameReward;
import com.yjhb.android.sbz.entity.reward.Reward;
import com.yjhb.android.sbz.mvp.reward.RewardPresenter;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.yjhb.android.sbz.view.RewardView;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.jojo.android.zxlib.view.dialog.ILoading;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;

/**
 * 创建者 zx
 * 创建时间 2021/1/15 15:22
 */
public class FragmentGameReward extends BaseFragment<RewardPresenter> implements com.yjhb.android.sbz.mvp.reward.RewardView {
    private final String TAG = "FragmentGameReward";
    @BindView(R.id.bottom_tv)
    TextView bottomTv;
    @BindView(R.id.reward2)
    RewardView reward2;
    @BindView(R.id.reward3)
    RewardView reward3;
    @BindView(R.id.receive_tv)
    TextView receiveTv;
    @BindView(R.id.empty_tv)
    TextView emptyTv;
    @BindView(R.id.emptyRecyclerView)
    EmptyRecyclerView emptyRecyclerView;
    @BindView(R.id.activity_time_tv)
    TextView activity_time_tv;

    private CountDownTimer countDownTimer;

    GameRewardAdapter adapter;

    final Integer TYPE = 3;


    GameReward gameReward;

    ILoading loading;

    @Override
    protected RewardPresenter initPresenter() {
        return new RewardPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_game_reward;
    }

    @Override
    protected void initUI() {
        loading = LoadingDialogTool.createDialog(this);
        emptyRecyclerView.setEmptyView(emptyTv);
        emptyRecyclerView.setAdapter(adapter = new GameRewardAdapter(getContext()));
        Calendar calendar = Calendar.getInstance();
        String time = new StringBuilder("活动时间：")
                .append(calendar.get(Calendar.YEAR))
                .append(".")
                .append(calendar.get(Calendar.MONTH) + 1)
                .append(".")
                .append("1")
                .append("-")
                .append(calendar.get(Calendar.YEAR))
                .append(".")
                .append(calendar.get(Calendar.MONTH) + 1)
                .append(".")
                .append(calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
                .toString();
        activity_time_tv.setText(time);
        countDownTimer();
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        loading.show();
        presenter.rankAgentInfo(TYPE);

    }

    private void countDownTimer() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        countDownTimer = new CountDownTimer(calendar.getTimeInMillis(), 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {

            }
        };
        countDownTimer.start();
    }

    @Override
    public void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        super.onDestroy();
    }

    @Override
    public void onUserRewardInfo(Object info) {
        gameReward = new Gson().fromJson(new Gson().toJson(info), GameReward.class);
        reward2.getBottomTv().setText("￥" + gameReward.getRecentReward().setScale(1, BigDecimal.ROUND_DOWN));
        reward3.getBottomTv().setText("￥" + new BigDecimal(gameReward.getRecentStageReward()).setScale(1, BigDecimal.ROUND_DOWN));
        loading.cancel();
        presenter.getStageInfo(TYPE);
    }

    @Override
    public void onGetSuccess() {

    }

    @Override
    public void onRewardRuleList(List<Reward> rewards) {
        for (Reward reward : rewards) {
            reward.setCurr(gameReward.getRecentReward().intValue());
        }
        adapter.setData(rewards);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onWait() {

    }

    @Override
    public void onFailure(int code, String msg) {
        loading.cancel();
        ZxToast.showINFOBottomToast(msg);
    }
}
